IrcJvn2.java

Go to the documentation of this file.
00001 package irc;
00002 
00003 /***
00004  * Irc class : simple implementation of a chat using JAVANAISE
00005  *
00006  * @author Fabienne BOYER,
00007  * @author Lionel DEBROUX, http://lionel.debroux.free.fr
00008  * @author Savas Ali TOKMEN, http://ali.tokmen.com
00009  */
00010 
00011 import java.awt.*;
00012 import java.awt.event.*;
00013 
00014 import irc.JvnSentence;
00015 
00016 import jvn.*;
00017 
00018 
00024 public class IrcJvn2 {
00025         protected TextArea text;
00026         protected TextField data;
00027         protected JvnSentence sentence;
00028 
00032         public static void main(String argv[]) {
00033                 try {
00034                         // initialize JVN
00035                         JvnServerImpl js = JvnServerImpl.jvnGetServer();
00036 
00037                         if (js == null) {
00038                                 System.out.println("IRC problem : cannot create server!");
00039                         }
00040                         else {
00041                                 // look up the IRC object in the JVN server
00042                                 // if not found, create it, and register it in the JVN server
00043                                 JvnSentence jo = (JvnSentence)(js.jvnLookupObject("IRC",JvnSentence.class));
00044                                 if (jo == null) {
00045                                         jo = new JvnSentence();
00046                                         // after creation, I have a write lock on the object
00047                                         jo.jvnUnLock();
00048                                         js.jvnRegisterObject("IRC", jo);
00049                                 }
00050                                 // create the graphical part of the Chat application
00051                                 new IrcJvn2(jo);
00052                         }
00053                 } catch (JvnException je) {
00054                         System.out.println("IRC problem : " + je.getMessage());
00055                 }
00056         }
00057 
00063         public IrcJvn2(JvnSentence jo) {
00064                 // Create frame
00065                 sentence = jo;
00066                 Frame frame=new Frame("Javanaise Client: IRC");
00067                 frame.addWindowListener(new WindowAdapter() {
00068                         public void windowClosing(WindowEvent e) {
00069                                 e.getWindow().dispose();
00070                                 try {
00071                                         JvnServerImpl.jvnGetServer().jvnTerminate();
00072                                 } catch (JvnException je) {}
00073                                 System.exit(0);
00074                         }
00075                 });
00076                 frame.setLayout(new BorderLayout(1,1));
00077 
00078                 // Create and add texts
00079                 Panel texts = new Panel(new GridLayout(1,1));
00080                 text=new TextArea(10,60);
00081                 text.setEditable(false);
00082                 text.setForeground(Color.red);
00083                 text.setBackground(Color.black);
00084                 texts.add(text);
00085                 data=new TextField(40);
00086                 texts.add(data);
00087 
00088                 // Create and add buttons
00089                 Panel buttons = new Panel(new GridLayout(1,1));
00090                 Button read_button = new Button("Read");
00091                 read_button.addActionListener(new ButtonListenerJvn2(this, ButtonType.Read));
00092                 buttons.add(read_button);
00093                 Button unlock_button = new Button("Unlock");
00094                 unlock_button.addActionListener(new ButtonListenerJvn2(this, ButtonType.Unlock));
00095                 buttons.add(unlock_button);
00096                 Button write_button = new Button("Write");
00097                 write_button.addActionListener(new ButtonListenerJvn2(this, ButtonType.Write));
00098                 buttons.add(write_button);
00099 
00100                 // Add texts + buttons and display frame
00101                 frame.add(texts, BorderLayout.CENTER);
00102                 frame.add(buttons, BorderLayout.SOUTH);
00103                 frame.setSize(500,200);
00104                 frame.setVisible(true);
00105         }
00106 }
00107 
00108 
00112 class ButtonListenerJvn2 implements ActionListener {
00113         IrcJvn2 irc;
00114         ButtonType button;
00115 
00122         public ButtonListenerJvn2 (IrcJvn2 i, ButtonType b) {
00123                 irc = i;
00124                 button = b;
00125         }
00126 
00132         public void actionPerformed (ActionEvent e) {
00133                 (new ButtonThreadJvn2(irc, button)).start();
00134         }
00135 }
00136 
00142 class ButtonThreadJvn2 extends Thread {
00143         private IrcJvn2 irc;
00144         private ButtonType button;
00145 
00152         ButtonThreadJvn2( IrcJvn2 i, ButtonType b ) {
00153                 irc = i;
00154                 button = b;
00155         }
00156 
00162         public void run() {
00163                 try {
00164                         if( button == ButtonType.Read ) {
00165                                 // invoke the method
00166                                 String s = irc.sentence.read();
00167 
00168                                 // display the read value
00169                                 irc.data.setText(s);
00170                                 irc.text.append(s+"\n");
00171 
00172                         } else if( button == ButtonType.Write ) {
00173                                 // get the value to be written from the buffer
00174                                 String s = irc.data.getText();
00175 
00176                                 // invoke the method
00177                                 irc.sentence.write(s);
00178                         }
00179                 } catch (JvnException je) {
00180                         System.out.println("Javanaise error while processing IRC button action: " + je.getMessage());
00181                 } catch( Exception e ) {
00182                         System.out.println( "Error while processing IRC button action: "+e);
00183                 }
00184         }
00185 }

Generated on Wed Jan 2 10:15:54 2008 for Javanaise by  doxygen 1.5.4